package com.bee.commons.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 日期工具
 * @author  Damin.yang
 * @date  2019/7/17 22:32
 */
public class DateUtil {
    public static final int ONE_DAY = 86400;
    public static final int ONE_HOUR = 3600;
    public static final int ONE_MINUTE = 60;
    public static final int ONE_SECOND = 1;
    public static final int SECOND_60 = 60;

    /**
     * 单位Millis， 10秒
     */
    public static final int TEN_SECONDS = 10000;
    public static final String DATETIME = "yyyy-MM-dd HH:mm:ss SSS";
    public static final String MILLISECOND = "yyyyMMddHHmmssSSS";
    public static final String CLOSE_SECONDS = "yyyyMMddHHmmss";
    public static final String CLOSE_DATE = "yyyyMMdd";

    public static final String PATTERN_MONTH_CH = "yyyy年MM月";
    public static final String PATTERN_MONTH = "yyyy-MM";
    public static final String PATTERN_DAY = "yyyy-MM-dd";
    public static final String PATTERN_DEFAULT = "yyyy-MM-dd HH:mm:ss";

    public static String formatDateTime(Date date) {
        return formatDate(date, PATTERN_DEFAULT);
    }

    public static String now() {
        return formatDate(new Date(), PATTERN_DEFAULT);
    }

    public static String formatMonth(Date date) {
        return formatDate(date, PATTERN_MONTH);
    }

    public static String formatDate(Date date) {
        return formatDate(date, PATTERN_DAY);
    }

    public static String formatDate(Date date, String pattern) {
        if (date == null) {
            return null;
        }
        if (pattern == null) {
            return null;
        }
        SimpleDateFormat formatter = new SimpleDateFormat(pattern);
        return formatter.format(date);
    }

    /**
     * 字符串转为Date
     * @param date
     * @param format
     * @return
     */
    public static Date parseToDate(String date, String format) {
        if (date == null) {
            return null;
        }
        SimpleDateFormat sm = new SimpleDateFormat(format);
        try {
            return sm.parse(date);
        } catch (ParseException e) {
            return null;
        }
    }

    /**
     * 字符串转为Date
     * @param date "yyyy-MM-dd"
     * @return
     */
    public static Date parseToDate(String date) {
        return parseToDate(date, PATTERN_DAY);
    }

    /**
     * 字符串转为Date
     * @param date "yyyy-MM-dd HH:mm:ss"
     * @return
     */
    public static Date parseToDateTime(String date) {
        return parseToDate(date, PATTERN_DEFAULT);
    }

}
